import { Modal, notification } from 'ant-design-vue';
import { h } from 'vue';
import { localizationService } from '/@/shared/i18n';
import GLayoutBottom from '/@/layouts/default/footer/g-layout-bottom/g-layout-bottom.vue';

export class MessageExtension {
  /**
   * 覆盖abp.message替换为 antd 的模态框
   */
  public static overrideAbpMessageByModal() {
    abp.message.info = (message: string, title?: string) => {
      Modal.info({
        title,
        content: () => {
          return h('div', {
            innerHTML: message,
          });
        },
        mask: true,
        closable: true,
      });
      // GLayoutBottom["methods"].changeMsg(message, title);
    };

    abp.message.warn = (message: string, title?: string) => {
      Modal.warning({
        title,
        content: () => {
          return h('div', {
            innerHTML: message,
          });
        },
        mask: true,
        closable: true,
      });
      // GLayoutBottom["methods"].changeMsg(message, title);
    };
    abp.message.error = (message: string, title?: string) => {
      Modal.error({
        title,
        content: () => {
          return h('div', {
            innerHTML: message,
          });
        },
        mask: true,
        closable: true,
      });
      // GLayoutBottom["methods"].changeMsg(message, title);
    };
    abp.message.success = (message: string, title?: string) => {
      Modal.success({
        title,
        content: () => {
          return h('div', {
            innerHTML: message,
          });
        },
        mask: true,
        closable: true,
      });
      // GLayoutBottom["methods"].changeMsg(message, title);
    };
    abp.message.confirm = this.confirm;
  }

  /**
   * 覆盖abp.message替换为 antd 的notify
   */
  public static overrideAbpNotify() {
    abp.notify.info = (message: string, title?: string, options?: any) => {
      if (!options) {
        options = {};
      }
      notification.info({
        message: title || localizationService.fanyi('Info'),
        description: () => {
          return h('div', {
            innerHTML: message,
          });
        },
        ...options,
      });
      abp.event.trigger('httpRequestSuccess', {
        type: 'Info',
        message: message,
        title: title,
        options: options,
      });
    };
    abp.notify.warn = (message: string, title?: string, options?: any) => {
      if (!options) {
        options = {};
      }
      notification.warn({
        message: title || localizationService.fanyi('Warn'),
        description: () => {
          return h('div', {
            innerHTML: message,
          });
        },
        ...options,
      });
      abp.event.trigger('httpRequestSuccess', {
        type: 'Warning',
        message: message,
        title: title,
        options: options,
      });
    };
    abp.notify.error = (message: string, title?: string, options?: any) => {
      if (!options) {
        options = {};
      }
      notification.error({
        message: title || localizationService.fanyi('Error'),
        description: () => {
          return h('div', {
            innerHTML: message,
          });
        },
        ...options,
      });
      abp.event.trigger('httpRequestSuccess', {
        type: 'Error',
        message: title,
        title: message,
        options: options,
      });
    };
    abp.notify.success = (message: string, title?: string, options?: any) => {
      if (!options) {
        options = {};
      }
      notification.success({
        message: title || localizationService.fanyi('Success'),
        description: () => {
          return h('div', {
            innerHTML: message,
          });
        },
        ...options,
      });
      abp.event.trigger('httpRequestSuccess', {
        type: 'Success',
        message: message,
        title: title,
        options: options,
      });
    };
  }

  // msg confirm
  private static confirm(message: string, callback?: (result: boolean) => void): any;

  private static confirm(
    message: string,
    title?: string,
    callback?: (result: boolean) => void,
  ): any;

  private static confirm(
    message: string,
    titleOrCallBack?: string | ((result: boolean) => void),
    callback?: (result: boolean) => void,
  ): any {
    if (typeof titleOrCallBack === 'string') {
      Modal.confirm({
        title: titleOrCallBack,
        content: () => {
          return h('span', {
            innerHTML: message,
          });
        },
        closable: true,
        onOk() {
          if (callback) callback(true);
        },
        onCancel() {
          if (callback) callback(false);
        },
      });
    } else {
      Modal.confirm({
        title: abp.localization.localize(
          'MessageConfirmOperation',
          abp.localization.defaultSourceName,
        ),
        content: () => {
          return h('div', {
            innerHTML: message,
          });
        },
        closable: true,
        onOk() {
          if (titleOrCallBack) titleOrCallBack(true);
        },
        onCancel() {
          if (titleOrCallBack) titleOrCallBack(false);
        },
      });
    }
  }
}
